from django.db import models

# Create your models here.
# 商品信息模型类
from django.db import models
from haoke_mall.utils.models import BaseModel
from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField

# 商品类别
class GoodsCategory(BaseModel):
    """
    商品类别：
    1，与商品频道，一对一：id---category_id
    2，与商品SKU，一对一：id---category_id
    3，与商品SPU，一对多：id---category1_id,category2_id,category3_id
    """
    name = models.CharField(max_length=10, verbose_name='名称')
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, verbose_name='父类别')

    class Meta:
        db_table = 'tb_goods_category'
        verbose_name = '商品类别'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 商品频道
class GoodsChannel(BaseModel):
    """
    商品频道：
    1，与商品类别，一对一：category_id--商品类别id
    """
    group_id = models.IntegerField(verbose_name='组号')
    # on_delete：用于定义当删除关联对象时，外键字段的行为
    # models.CASCADE级联删除：当删除关联对象时，与之关联的外键对象也会被删除
    category = models.ForeignKey(GoodsCategory, on_delete=models.CASCADE, verbose_name='顶级商品类别')
    url = models.CharField(max_length=50, verbose_name='频道页面链接')
    sequence = models.IntegerField(verbose_name='组内顺序')

    class Meta:
        db_table = 'tb_goods_channel'
        verbose_name = '商品频道'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.category.name

# 商品品牌
class GoodsBrand(BaseModel):
    """
    商品品牌：
    1，与商品SPU，一对多：id---brand_id
    """
    name = models.CharField(max_length=20, verbose_name='名称')
    logo = models.ImageField(verbose_name='Logo图片')
    first_letter = models.CharField(max_length=1, verbose_name='品牌首字母')

    class Meta:
        db_table = 'tb_goods_brand'
        verbose_name = '品牌'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 商品SPU（Standard Product Unit （标准产品单位））
class GoodsSPU(BaseModel):
    """
    商品SPU：(属性特征，成品名称：iPhone15是商品的SPU)
    1，与商品类别，三组多对一：category1_id---商品类别id,category2_id---商品类别id,category3_id---商品类别id
    2，与商品品牌，多对一：brand_id---商品品牌id
    3，与商品SKU，一对多：id---goods_id
    4，与商品规格，一对多：id---goods_id
    """
    name = models.CharField(max_length=50, verbose_name='名称')
    brand = models.ForeignKey(GoodsBrand, on_delete=models.PROTECT, verbose_name='品牌')
    category1 = models.ForeignKey(GoodsCategory, on_delete=models.PROTECT, related_name='cat1_goods', verbose_name='一级类别')
    category2 = models.ForeignKey(GoodsCategory, on_delete=models.PROTECT, related_name='cat2_goods', verbose_name='二级类别')
    category3 = models.ForeignKey(GoodsCategory, on_delete=models.PROTECT, related_name='cat3_goods', verbose_name='三级类别')
    sales = models.IntegerField(default=0, verbose_name='销量')
    comments = models.IntegerField(default=0, verbose_name='评价数')

    # 使用富文本编辑器
    desc_detail = RichTextUploadingField(default='', verbose_name='详细介绍')
    desc_pack = RichTextField(default='', verbose_name='包装信息')
    desc_service = RichTextUploadingField(default='', verbose_name='售后服务')
    class Meta:
        db_table = 'tb_goods_spu'
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

# 商品规格
class GoodsSpecification(BaseModel):
    """
    商品规格：
    1，与商品SKU具体规格，一对多：id---spec_id
    2，与商品SPU，多对一：goods_id---商品SPU的id
    3，与商品规格选项，一对多，id---spec_id
    """
    goods = models.ForeignKey(GoodsSPU, on_delete=models.CASCADE, verbose_name='商品')
    name = models.CharField(max_length=20, verbose_name='规格名称')

    class Meta:
        db_table = 'tb_goods_specification'
        verbose_name = '商品规格'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s: %s' % (self.goods.name, self.name)
# 商品规格选项
class SpecificationOption(BaseModel):
    """
    规格选项：
    1，与商品规格，多对一：spec_id---商品规格id
    2，与商品SKU具体规格，一对多：id---option_id
    """
    spec = models.ForeignKey(GoodsSpecification, on_delete=models.CASCADE, verbose_name='规格')
    value = models.CharField(max_length=20, verbose_name='选项值')

    class Meta:
        db_table = 'tb_specification_option'
        verbose_name = '规格选项'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s - %s' % (self.spec, self.value)

# 商品SKU（Stock Keeping Unit （库存量单位））
class GoodsSKU(BaseModel):
    """
    商品SKU：
    1，与商品SPU，多对一：goods_id---商品SPU的id
    2，与商品类型，多对一：category_id---商品类型id
    3，与商品SKU具体规格，一对多：id---sku_id
    4，与SKU图片，一对多：id---sku_id
    """
    name = models.CharField(max_length=50, verbose_name='名称')
    caption = models.CharField(max_length=100, verbose_name='副标题')
    goods = models.ForeignKey(GoodsSPU, on_delete=models.CASCADE, verbose_name='商品') # 关联商品SPU
    category = models.ForeignKey(GoodsCategory, on_delete=models.PROTECT, verbose_name='从属类别')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')
    cost_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='进价')
    market_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='市场价')
    stock = models.IntegerField(default=0, verbose_name='库存')
    sales = models.IntegerField(default=0, verbose_name='销量')
    comments = models.IntegerField(default=0, verbose_name='评价数')
    is_launched = models.BooleanField(default=True, verbose_name='是否上架销售')
    default_image_url = models.CharField(max_length=200, default='', null=True, blank=True, verbose_name='默认图片')

    class Meta:
        db_table = 'tb_goods_sku'
        verbose_name = '商品SKU'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s: %s' % (self.id, self.name)
# SKU图片
class SKUImage(BaseModel):
    """
    SKU图片：
    1，与商品SKU，多对一：sku_id---商品SKU的id
    """
    sku = models.ForeignKey(GoodsSKU, on_delete=models.CASCADE, verbose_name='sku')
    image = models.ImageField(verbose_name='图片')

    class Meta:
        db_table = 'tb_sku_image'
        verbose_name = 'SKU图片'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s %s' % (self.sku.name, self.id)

# 商品SKU具体规格
class GoodsSKUSpecification(BaseModel):
    """
    商品SKU具体规格:
    1，与商品SKU，多对一：sku_id---商品SKU的id
    2，与商品规格，多对一：spec_id---商品规格id
    3，与商品规格选项，多对一：option_id---商品规格选项id
    """
    sku = models.ForeignKey(GoodsSKU, on_delete=models.CASCADE, verbose_name='sku')
    spec = models.ForeignKey(GoodsSpecification, on_delete=models.PROTECT, verbose_name='规格名称')
    option = models.ForeignKey(SpecificationOption, on_delete=models.PROTECT, verbose_name='规格值')

    class Meta:
        db_table = 'tb_goods_sku_specification'
        verbose_name = 'SKU规格'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s: %s - %s' % (self.sku, self.spec.name, self.option.value)

# 广告内容类别
class ADContentCategory(BaseModel):
    """
    广告内容类别
    1，与广告内容，一对多：id---category_id
    """
    name = models.CharField(max_length=50, verbose_name='名称')
    key = models.CharField(max_length=50, verbose_name='类别键名')

    class Meta:
        db_table = 'tb_ad_content_category'
        verbose_name = '广告内容类别'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
# 广告内容
class ADContent(BaseModel):
    """
    广告内容
    1，与广告内容类别，多对一：category_id---广告内容类别id
    """
    category = models.ForeignKey(ADContentCategory, on_delete=models.PROTECT, verbose_name='类别')
    title = models.CharField(max_length=100, verbose_name='标题')
    url = models.CharField(max_length=300, verbose_name='内容链接')
    image = models.ImageField(null=True, blank=True, verbose_name='图片')
    text = models.TextField(null=True, blank=True, verbose_name='内容')
    sequence = models.IntegerField(verbose_name='排序')
    status = models.BooleanField(default=True, verbose_name='是否展示')

    class Meta:
        db_table = 'tb_ad_content'
        verbose_name = '广告内容'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.category.name + ': ' + self.title